﻿#ifndef SYSTEMSETTING_H
#define SYSTEMSETTING_H

#include <QWidget>
#include <QDebug>
#include <QSerialPortInfo>
#include <QSerialPort>
#include <QMutex>
#include <QMutexLocker>
#include <QSettings>
#include <QCoreApplication>

#define LOG_PATH QCoreApplication::applicationDirPath()

namespace Ui {
class SystemSetting;
}

class SystemSetting : public QWidget
{
    Q_OBJECT

private:
    explicit SystemSetting(QWidget *parent = nullptr);
    SystemSetting(const SystemSetting&) = delete;
    SystemSetting& operator=(const SystemSetting&) = delete;
    void InitSystemSet(void);

public:
    ~SystemSetting();
    // 获取单例实例（线程安全）
    static SystemSetting* instance(QWidget *parent = nullptr);
    //获取设置（线程安全）
    QString serialName() const;
    QSerialPort::BaudRate serialBaud() const;
    qint32 timeout() const;
    bool autoMode() const;
    void setSerialOpened(const bool isopen);

private slots:
    void on_btn_saveSet_clicked();
    void slots_serialRefresh();

signals:
    void sig_changeSerial(QString port_name,int baud_rate);
    void sig_changeTimeout(qint32 timeout);
    void sig_changeAutoMode(bool mode);

private:
    Ui::SystemSetting *ui;

    // 设置数据（使用 mutable 保证 const 方法的线程安全）
    mutable QMutex m_mutex;
    QStringList m_serialList;
    QString m_serialName = "";
    int m_serialBaud;
    bool m_serialOpened;
    qint32 m_timeout = 0;
    bool m_autoMode = false;

    // 单例实例指针
    static SystemSetting* m_instance;
    static QMutex m_instanceMutex;
};

#endif // SYSTEMSETTING_H
